package com.zyy.seckill.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zyy.seckill.entity.User;
import com.zyy.seckill.vo.RespBean;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;

/**
 * 用户生成工具
 *
 * @Author: zyy
 * @DateTime: 2021/10/4 21:07
 */
public class UserUtil {
    public static void main(String[] args) throws Exception {
         createUser(5000);
    }

    public static void createUser(int count) throws Exception {
        ArrayList<User> users = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            User user = new User();
            user.setId(13000000095L + i);
            user.setNickname("user" + i);
            user.setPassword(MD5Util.inputPassToDBPass("123456", MD5Util.SALT));
            user.setSlat(MD5Util.SALT);
            users.add(user);
        }
        System.out.println("Create user");

        Connection connection = getConn();
        String sql = "insert into t_user(id,nickname,slat,password)values(?,?,?,?)";
        PreparedStatement statement
                = connection.prepareStatement(sql);
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            statement.setLong(1,user.getId());
            statement.setString(2,user.getNickname());
            statement.setString(3,user.getSlat());
            statement.setString(4,user.getPassword());
            statement.addBatch();
        }
        statement.executeBatch();
        statement.close();
        connection.close();
        System.out.println("into db");
        // 登录 生成userTicket
         String urlString = "http://localhost:8080/login/doLogin";
         File file = new File("C:\\Users\\18090\\Desktop\\config.txt");
         if (file.exists()) {
             file.delete();
       }
         RandomAccessFile raf = new RandomAccessFile(file, "rw");
         file.createNewFile();
         raf.seek(0);
         for (int i = 0; i < users.size(); i++) {
             User user = users.get(i);
             URL url = new URL(urlString);
             HttpURLConnection co = (HttpURLConnection) url.openConnection();
             co.setRequestMethod("POST");
             co.setDoOutput(true);
             OutputStream out = co.getOutputStream();
             String params = "mobile=" + user.getId() + "&password=" +MD5Util.inputPassToFormPass("123456",MD5Util.SALT);
             out.write(params.getBytes());
             out.flush();
             InputStream inputStream = co.getInputStream();
             ByteArrayOutputStream bout = new ByteArrayOutputStream();
             byte buff[] = new byte[1024];
             int len = 0;
             while ((len = inputStream.read(buff)) >= 0) {
                  bout.write(buff, 0, len);
             }
             inputStream.close();

             bout.close();
             String response = new String(bout.toByteArray());
             ObjectMapper mapper = new ObjectMapper();
             RespBean respBean = mapper.readValue(response, RespBean.class);
             String userTicket = ((String) respBean.getObj());
             System.out.println("create userTicket : " + user.getId());
             String row = user.getId() + "," + userTicket;
             raf.seek(raf.length());
             raf.write(row.getBytes());
             raf.write("\r\n".getBytes());
             System.out.println("write to file : " + user.getId());
       }
         raf.close();
         System.out.println("over");


    }

    private static Connection getConn() throws Exception {

        String url = "jdbc:mysql://123.57.10.246:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "root";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);

        return DriverManager.getConnection(url, username, password);
    }
}
